package com.sx.micowell.module.member

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.GridItemDecoration
import com.gyf.immersionbar.ImmersionBar
import com.example.mvvm.bar.TitleBar
import com.miloyu.modelnet.response.UserInfoVO
import com.miloyu.modelnet.response.getUrl
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragOpenMemberBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.MemberModel

/**
 * @author: YY
 * @time: 2021/8/9
 * @describe 开通会员
 */
@Route(path = RoutePath.OPEN_MEMBER)
class MemberFragment : MyFragment<FragOpenMemberBinding, ContainerActivity>(), TitleBarAction {

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var userInfo: UserInfoVO? = null

    lateinit var viewModel: MemberModel

    private var memberSetMealListAdapter: MemberSetMealListAdapter? = null

    override fun initViewModel() {
        viewModel = getFragmentScopeViewModel(MemberModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_open_member, BR.viewModel, viewModel)
    }

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    override fun initData() {
        initVIP()
        viewModel.memberSetMeal()
    }

    fun initVIP(){
        if (userInfo != null) {
            // 图片
            if (!TextUtils.isEmpty(userInfo!!.userPhoto)) {
                Glide.with(this).load(userInfo!!.userPhoto.getUrl())
                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    .placeholder(R.drawable.icon_head_portrait)
                    .into(binding.imageHeadPortrait)
            }
            binding.tvUserName.text = userInfo!!.userPhone
            if (userInfo!!.isVip) {
                binding.tvVipMsg.text = "您已是会员"
                binding.tvOpen.text="立即续费"
            } else {
                binding.tvVipMsg.text = "您还不是会员，开通立享会员特权"
                binding.tvOpen.text="立即开通"
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        // 会员套餐列表
        setMemberSetMealList()
        viewModel?.userInfoEvent.observe(this){
            userInfo=it
            initVIP()
        }
    }

    override fun getTitleBar(): TitleBar {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        mActivity.finish()
    }

    override fun onStart() {
        super.onStart()
        viewModel?.userInfo()
    }

    /**
     * 套餐列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun setMemberSetMealList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerMember.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerMember.layoutManager = GridLayoutManager(context, 2)
        binding.recyclerMember.addItemDecoration(GridItemDecoration(15, 10))

        viewModel.memberSetMealEvent.observe(this,  {
            memberSetMealListAdapter = MemberSetMealListAdapter()
            memberSetMealListAdapter?.data = it
            binding.recyclerMember.adapter = memberSetMealListAdapter

            binding.tvVipContent.text = it[0].synopsis
            viewModel.memberSetMeal = it[0]

            memberSetMealListAdapter?.setOnItemClickListener { adapter, view, position ->
                memberSetMealListAdapter?.clickId = position
                adapter.notifyDataSetChanged()

                viewModel.memberSetMeal = it[position]

                binding.tvVipContent.text = it[position].synopsis
            }
        })
    }
}